package com.bookstore.controller;

import com.bookstore.dto.BookDTO;
import com.bookstore.entity.User;
import com.bookstore.entity.UserHistory;
import com.bookstore.service.UserHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/history")
public class UserHistoryController {
    
    @Autowired
    private UserHistoryService userHistoryService;
    
    @PostMapping("/add")
    public ResponseEntity<?> addHistory(@RequestBody Map<String, Object> payload) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated() || !(auth.getPrincipal() instanceof User)) {
            return ResponseEntity.badRequest().body("用户未登录");
        }
        
        Long userId = ((User) auth.getPrincipal()).getId();
        Long bookId = Long.parseLong(payload.get("bookId").toString());
        String actionType = payload.get("actionType").toString();
        
        UserHistory history = userHistoryService.addHistory(userId, bookId, actionType);
        return ResponseEntity.ok(history);
    }
    
    @GetMapping
    public ResponseEntity<?> getUserHistory() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated() || !(auth.getPrincipal() instanceof User)) {
            return ResponseEntity.badRequest().body("用户未登录");
        }
        
        Long userId = ((User) auth.getPrincipal()).getId();
        List<UserHistory> history = userHistoryService.getUserHistory(userId);
        return ResponseEntity.ok(history);
    }
    
    @GetMapping("/viewed-books")
    public ResponseEntity<?> getUserViewedBooks(@RequestParam(defaultValue = "10") int limit) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated() || !(auth.getPrincipal() instanceof User)) {
            return ResponseEntity.badRequest().body("用户未登录");
        }
        
        Long userId = ((User) auth.getPrincipal()).getId();
        List<BookDTO> books = userHistoryService.getUserViewedBooks(userId, limit);
        return ResponseEntity.ok(books);
    }
    
    @DeleteMapping
    public ResponseEntity<?> clearUserHistory() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated() || !(auth.getPrincipal() instanceof User)) {
            return ResponseEntity.badRequest().body("用户未登录");
        }
        
        Long userId = ((User) auth.getPrincipal()).getId();
        userHistoryService.clearUserHistory(userId);
        return ResponseEntity.ok().build();
    }
}